package com.jdz.gxq.info.common.bo;

import jp.terasoluna.fw.util.PropertyUtil;

public class PageBean {

	private static String PAGE_LINKS_PAGE_SIZE = "pageLinks.page.size";

	/**
	 * 数据总行数
	 */
	private int totalCount;

	/**
	 * 每页行数
	 */
	private int pageSize;

	/**
	 * 当前页码
	 */
	private int currentPage = 1;

	/**
	 * 上一页页码
	 */
	private int nextPage;

	/**
	 * 下一页页码
	 */
	private int previousPage;

	/**
	 * 总页数
	 */
	private int totalPageCount;

	/**
	 * 是否有下一页
	 */
	private boolean hasNext;

	/**
	 * 是否有前一页
	 */
	private boolean hasPrevious;

	/**
	 * 当前页开始行数
	 */
	private int pageStartRow;

	/**
	 * 当前页结束行数
	 */
	private int pageEndRow;

	public PageBean(int totalRows, int currentPage, int pageSzie) {
		this.setPageSize(pageSzie);
		setTotalCount(totalRows);
		setCurrentPage(currentPage);
	}

	public PageBean(int totalRows, int currentPage) {

		this.setPageSize(Integer.parseInt(PropertyUtil
				.getProperty(PAGE_LINKS_PAGE_SIZE)));
		setTotalCount(totalRows);
		setCurrentPage(currentPage);
	}

	public PageBean() {
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;

		if (totalCount % pageSize == 0) {
			totalPageCount = totalCount / pageSize;
		} else {
			totalPageCount = totalCount / pageSize + 1;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int pCurrentPage) {

		// 如果请求的当前页大于总页数,则把最后一页作为当前页
		if (pCurrentPage > totalPageCount) {
			pCurrentPage = totalPageCount;
		}

		if (pCurrentPage <= 0) {
			pCurrentPage = 1;
		}

		this.currentPage = pCurrentPage;

		this.previousPage = currentPage - 1;

		this.nextPage = currentPage + 1;

		// 计算当前页开始行和结束行
		if (currentPage * pageSize < totalCount) {

			// 先计算结束行，然后用结束页推算前一页
			pageEndRow = currentPage * pageSize;
			pageStartRow = pageEndRow - pageSize + 1;
		} else {
			pageEndRow = totalCount;
			pageStartRow = pageSize * (totalPageCount - 1) + 1;
		}

		if (pageStartRow < 1) {
			pageStartRow = 1;
		}

		// 是否存在前页和后页
		if (nextPage > totalPageCount) {
			hasNext = false;
		} else {
			hasNext = true;
		}
		if (previousPage <= 0) {
			hasPrevious = false;
		} else {
			hasPrevious = true;
		}
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getPreviousPage() {
		return previousPage;
	}

	public void setPreviousPage(int previousPage) {
		this.previousPage = previousPage;
	}

	public int getTotalPageCount() {
		return totalPageCount;
	}

	public void setTotalPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public boolean isHasPrevious() {
		return hasPrevious;
	}

	public void setHasPrevious(boolean hasPrevious) {
		this.hasPrevious = hasPrevious;
	}

	public int getPageStartRow() {
		return pageStartRow;
	}

	public void setPageStartRow(int pageStartRow) {
		this.pageStartRow = pageStartRow;
	}

	public int getPageEndRow() {
		return pageEndRow;
	}

	public void setPageEndRow(int pageEndRow) {
		this.pageEndRow = pageEndRow;
	}
}
